package ru.compft.common.dao.impl;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Repository;
import ru.compft.common.dao.RoleDao;
import ru.compft.common.data.AppRole;
import ru.compft.common.data.meta.RoleProperty;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: VVasiliev
 * Date: 24.09.12
 * Time: 13:44
 * DAO работы с ролями пользователя
 */
@Repository
public class RoleDaoImpl implements RoleDao {
    private static final Logger logger = LoggerFactory.getLogger(RoleDaoImpl.class);

    @Resource(name = "sessionFactory")
    private SessionFactory sessionFactory;

    @Override
    public List<AppRole> getAllRoles() {
        Session session = sessionFactory.getCurrentSession();
        Criteria criteria = session.createCriteria(AppRole.class);

        return criteria.list();
    }

    @Override
    public List<AppRole> getRoles(int from, int count) {
        Session session = sessionFactory.getCurrentSession();
        Criteria criteria = session.createCriteria(AppRole.class)
                .setFirstResult(from)
                .setMaxResults(count);

        return criteria.list();
    }

    @Override
    public int countAllRoles() {
        Session session = sessionFactory.getCurrentSession();
        Criteria criteria = session.createCriteria(AppRole.class)
                .setProjection(Projections.rowCount());

        final Object result = criteria.uniqueResult();
        return result != null ? Integer.parseInt(result.toString()) : 0;
    }

    @Override
    public AppRole getRoleById(Long roleId) {
        Session session = sessionFactory.getCurrentSession();
        return (AppRole) session.get(AppRole.class, roleId);
    }

    /**
     * Получение роли по коду
     *
     * @param code
     * @return
     */
    @Override
    public AppRole getRoleByCode(String code) {
        Session session = sessionFactory.getCurrentSession();
        Criteria criteria = session.createCriteria(AppRole.class)
                .add(Restrictions.like(RoleProperty.code, code));

        return (AppRole) criteria.uniqueResult();
    }

    /**
     * Обновляем данные о роли
     *
     * @param newRole
     * @return
     */
    @Override
    public AppRole update(AppRole newRole) {
        return (AppRole) sessionFactory.getCurrentSession().merge(newRole);
    }

    /**
     * Сохраняем новую роль
     *
     * @param role
     * @return
     */
    @Override
    public AppRole persist(AppRole role) {
        sessionFactory.getCurrentSession().persist(role);
        return role;
    }

    /**
     * Удаляем роль
     *
     * @param role
     * @return
     */
    @Override
    public Boolean delete(AppRole role) {
        sessionFactory.getCurrentSession().delete(role);
        return true;
    }
}
